package com.kaikeba.service;

import com.kaikeba.bean.Expressman;
import com.kaikeba.dao.BaseExpressmanDao;
import com.kaikeba.dao.imp.ExpressmanDaoMysql;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class ExpressmanService {
    private static BaseExpressmanDao dao = new ExpressmanDaoMysql();

    public static Map<String, Integer> console() {
        return dao.console();
    }

    public static boolean insert(Expressman man) {
        try{
            if(man.getId() == null || man.getId() == ""){
                man.setId(generateId());
            }
            return dao.insert(man);
        }catch (Exception e){
            man.setId(generateId());
            return insert(man);
        }
    }

    public static boolean delete(String id) {
        return dao.delete(id);
    }

    public static boolean deleteByPhone(String phone){
        return dao.deleteByPhone(phone);
    }

    public static boolean updateByPhone(String phone, Expressman man) {
        return dao.updateByPhone(phone, man);
    }
    
    public static List<Expressman> findAll(int limit, int offset, int pageNumber) {
        return dao.findAll(limit, offset, pageNumber);
    }

    public static Expressman findByPhone(String phone) {
        return dao.findByPhone(phone);
    }

    public static String generateId(){
        int result = new Random().nextInt(900000) + 100000;
        return Integer.toString(result);
    }
}
